package com.maoslv.service.menu;

import com.maoslv.mapper.MenuMapper;
import com.maoslv.pojo.Menu;
import com.maoslv.utils.RedisUtil;
import com.maoslv.utils.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Principal;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public List<Menu> getMenuList(Integer id) {
        return menuMapper.getMenuList(id);
    }

    @Override
    public List<Integer> getMenuListById(Integer id) {
        return menuMapper.getMenuListById(id);
    }

    @Override
    public List<Menu> getAllMenuList() {
        List<Menu> allMenuList = menuMapper.getAllMenuList();
        List<Menu> treeMenu = TreeUtil.toTree(allMenuList);
        return treeMenu;
    }

    @Override
    public Integer getType(Integer parentId) {
        return menuMapper.getType(parentId);
    }

    @Override
    public int addMenu(Menu menu, Principal principal) {
        redisUtil.del(principal.getName() + "-menuList");
        menuMapper.addMenu(menu);
        return menuMapper.addMenuByRole(menu.getId());
    }

    @Override
    public Boolean haveChild(Integer id) {
        List<Menu> menuList = menuMapper.haveChild(id);
        if (menuList.size() == 0) {
            return false;
        }
        return true;
    }

    @Override
    public int delMenu(Integer id, Principal principal) {
        redisUtil.del(principal.getName() + "-menuList");
        return menuMapper.delMenu(id);
    }

    @Override
    public Menu getInfo(Integer id) {
        return menuMapper.getInfo(id);
    }

    @Override
    public int updateMenu(Map<String, Object> menuMap, Principal principal) {
        redisUtil.del(principal.getName() + "-menuList");
        return menuMapper.updateMenu(menuMap);
    }
}
